﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;

namespace blackhouse.BlackApart.Data.Sql {
	public class FurnitureDataAccess : AdoFurnitureDataAccess {

		#region Fields

		private string connectionString;
		private MsSqlAdoDataHelper dataHelper;

		#endregion

		#region Constructors

		public FurnitureDataAccess(string sqlConnectionString) {
			this.connectionString = sqlConnectionString;
			this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
			SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
		}

		#endregion

		#region AdoFurnitureDataAccess Members

		public override int SaveFurniture(int id, string name, string description, int sort, string imageName, byte[] imageData) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_FurnitureSave";
			this.dataHelper.AddParameter("Id", id, DbType.Int32);
			this.dataHelper.AddParameter("Name", name, DbType.String);
			this.dataHelper.AddParameter("Description", description, DbType.String);
			this.dataHelper.AddParameter("Sort", sort, DbType.Int32);
			this.dataHelper.AddParameter("ImageName", imageName, DbType.String);
			this.dataHelper.AddParameter("ImageData", imageData, DbType.Binary);
			return Convert.ToInt32(this.dataHelper.ExecuteScalar());
		}

		public override bool AddFurnitureToApartment(int furnitureId, int apartmentId) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_FurnitureToApartmentAdd";
			this.dataHelper.AddParameter("FurnitureId", furnitureId, DbType.Int32);
			this.dataHelper.AddParameter("ApartmentId", apartmentId, DbType.Int32);
			return this.dataHelper.ExecuteWithoutResult() > 0;
		}

		protected override IDataReader GetReaderForFurniture(int furnitureId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_FurnitureGet";
            this.dataHelper.AddParameter("Id", furnitureId, DbType.Int32);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
            return this.dataHelper.Execute();
        }

		protected override IDataReader GetReaderForApartmentFurnitures(int apartmentId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_FurnitureToApartmentGets";
			this.dataHelper.AddParameter("ApartmentId", apartmentId, DbType.Int32);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			return this.dataHelper.Execute();
		}

		protected override IDataReader GetReaderForApartmentTypeFurnitures(int apartmentTypeId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_FurnitureToApartmentTypeGets";
			this.dataHelper.AddParameter("ApartmentTypeId", apartmentTypeId, DbType.Int32);
			this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength, 5);
			return this.dataHelper.Execute();
		}

		#endregion

	}
}
